//
//  ShareSelfViewController.m
//  baobaotong
//
//  Created by lk on 16/6/21.
//  Copyright © 2016年 zzy. All rights reserved.
//

#import "ShareViewController.h"
#import "UIImageView+WebCache.h"
#import <UMSocialCore/UMSocialCore.h>
#import "MBProgressHUD+PKX.h"
#import "ShareFunction.h"

@interface ShareViewController ()

@end

@implementation ShareViewController


-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"推荐好友";
    
    if (self.localImgStr.length) {
        self.shareImageView.image = [UIImage imageNamed:self.localImgStr];
    }
    else if (self.linkImgStr.length) {
        [self.shareImageView sd_setImageWithURL:[NSURL URLWithString:self.linkImgStr]];
    }
    else {
        [MBProgressHUD showError:@"请设置扫描下载图片!" toView:self.navigationController.view];
    }
    
    [self config_Gestures];
}

- (void)config_Gestures
{
    UITapGestureRecognizer *wxtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(platformType:)];
    _weiXin.userInteractionEnabled = YES;
    [_weiXin addGestureRecognizer:wxtap];
    
    UITapGestureRecognizer *wfritap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(platformType:)];
    _wfriend.userInteractionEnabled = YES;
    [_wfriend addGestureRecognizer:wfritap];
    
    UITapGestureRecognizer *QQtap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(platformType:)];
    _QQ.userInteractionEnabled = YES;
    [_QQ addGestureRecognizer:QQtap];
    
    UITapGestureRecognizer *Zonetap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(platformType:)];
    _Zone.userInteractionEnabled = YES;
    [_Zone addGestureRecognizer:Zonetap];
}


- (void)platformType:(UITapGestureRecognizer *)tap
{
    [self shareToPlatformType:tap.view.tag];
}

- (void)shareToPlatformType:(NSInteger )types
{
    [ShareFunction shareInstance].titles = self.titles;
    [ShareFunction shareInstance].content = self.content;
    [ShareFunction shareInstance].iconImg = self.iconImg;
    [ShareFunction shareInstance].url = self.url;
    [[ShareFunction shareInstance] shareToPlatformType:types ToController:self];
}

- (void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    NSDictionary *dict = (NSDictionary *)obj;
    self.localImgStr = dict[@"localImg"];
    self.titles = dict[@"titles"];
    self.content = dict[@"content"];
    self.iconImg = [UIImage imageNamed:dict[@"iconImg"]];
    self.url = dict[@"url"];
}

#pragma mark ModuleProtocol代理回调方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //处理需要初始化的东西，比如这个是地图模块，这里就实现地图的初始化
    /* 打开调试日志 */
    [[UMSocialManager defaultManager] openLog:NO];
    
    /* 设置友盟appkey */
    [[UMSocialManager defaultManager] setUmSocialAppkey:UMAPP_ID];
    
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:Application_Types == 0 ? WX_AppID : WX_AppID_Store appSecret:Application_Types == 0 ? WX_AppSecret : WX_AppSecret_Store redirectURL:@"www.baidu.com"];
    /*
     * 移除相应平台的分享，如微信收藏
     */
    //[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
    
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名，统一用appKey和appSecret进行参数设置，而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:Application_Types == 0 ? QQ_AppID : QQ_AppID_Store appSecret:Application_Types == 0 ? QQ_AppSecret : QQ_AppSecret_Store redirectURL:nil];
    return YES;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
    if (!result) {
        // 其他如支付等SDK的回调
    }
    return result;
}

@end
